﻿using Company.Core.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace Company.Hardware.Detector
{
    public abstract class DetectorBase : IDetector
    {
        public DetectorConfig DetectorConfig { get;private set; }
        public bool Initialized {get;private set;}
        public bool IsGrabbing { get; set; }  

        public event Action<DetectorImage> OnGrabbed;

        public bool Close()
        {
            if (Initialized)
            {
                if (DoClose())
                {
                    return true;
                }
            }
            return false;
        }


        public bool Init(DetectorConfig config)
        {
            this.DetectorConfig = config;
            if (Initialized)
            {
                throw new Exception("重复初始化平板探测器");

            }
            if (DoInit())
            {
                return true;
            }
            return false;


        }
        public bool Grab()
        {
            if (!Initialized)
            {
                throw new Exception("未初始化平板探测器");
            }
            if (DoGrab())
            {
                IsGrabbing = true;
                return true;
            }
            return false;
        }

        protected void InvokeOnGrabbed(UnmanagedArray2D<ushort> image)
        {
            if (OnGrabbed == null) return;
            foreach (Action<DetectorImage> item in OnGrabbed.GetInvocationList())
            {
                item.Invoke(new DetectorImage(image));
            }
        }




        public abstract bool DoGrab();
        public abstract bool DoClose();
        public abstract bool DoInit();


    }
}
